package com.westa.smallshop.cart;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseListAdapter;
import com.westa.smallshop.common.BaseListFragment;
import com.westa.smallshop.common.BasePresenter;
import com.westa.smallshop.common.GetPresenterInterface;
import com.westa.smallshop.common.RecyclerViewBaseAdapter;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.util.ListUtils;

import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by cy on 10/11/16.
 */

public class CartFragment extends BaseListFragment<CartContract.Presenter, GoodsItem>
        implements CartContract.View<GoodsItem>, RecyclerViewBaseAdapter.OnItemClickListener, GetPresenterInterface {

    @BindString(R.string.title_cart)
    String mTitle;
    @BindView(R.id.tv_goods_detail_cart_price)
    TextView mTvGoodsDetailCartPrice;
    @BindView(R.id.btn_cart_confirm)
    Button mBtnCartConfirm;
    @BindView(R.id.tv_no_content)
    TextView mTvNoContent;
    @NonNull
    private CartContract.Presenter mPresenter;
    private CartAdapter mAdapter;

    private int deletePosition;

    public static CartFragment newInstance() {
        return new CartFragment();
    }

    @Override
    public void showDeleteSuccessful() {
        showSingleToast(R.string.general_delete_successful);
        mAdapter.removeItem(deletePosition);
    }

    @Override
    public void showDeleteFiled() {
        showSingleToast(R.string.general_delete_failed);
    }

    @Override
    public void initView() {
        super.initView();
        getActivity().setTitle(mTitle);
    }

    @Override
    public int getCreateViewLayoutRes() {
        return R.layout.fragment_cart_list;
    }

    @NonNull
    @Override
    public BaseListAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new CartAdapter(this);
            mAdapter.setOnItemClickListener(this);
//            final int m = DensityUtils.dp2px(getContext(), 4.0f);
//            getRecyclerView().addItemDecoration(new RecyclerView.ItemDecoration() {
//                @Override
//                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//                    outRect.set(m, m, m, m);
//                }
//            });
        }
        return mAdapter;
    }

    @Override
    public void refreshData() {
        mPresenter.refreshData();
    }

    @Override
    public void loadMoreData(int currentPage) {

    }

    void showDeleteConfirmDialog(final int position) {
        deletePosition = position;
        new MaterialDialog.Builder(getContext())
                .content("确定将这件物品从购物车中删除？")
                .positiveText(R.string.general_confirm)
                .negativeText(R.string.general_cancel)
                .positiveColor(Color.RED)
                .negativeColor(Color.GRAY)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        mPresenter.deleteFromCart(mAdapter.getItemObject(position).getGoods_id());
                    }
                })
                .show();
    }

    @Override
    public void refreshData(List<GoodsItem> list) {
        if (ListUtils.isEmpty(list)) {
            mTvNoContent.setVisibility(View.VISIBLE);
        } else {
            mTvNoContent.setVisibility(View.GONE);
        }
        mAdapter.refreshData(list);
        mTvGoodsDetailCartPrice.setText(String.valueOf(mAdapter.getPriceSum()));
        if (mAdapter.getItemCount() <= 0) {
            mBtnCartConfirm.setEnabled(false);
        }
    }

    @Override
    public void loadMoreData(List<GoodsItem> list) {

    }

    @Override
    public void getDataFailed(String errorMsg) {

    }

    @Override
    public void showOrderSubmitDialog() {
        showDialogProgress(R.string.dlg_order_submit_hint, new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {

            }
        });
    }

    @Override
    public void dismissDialog() {
        super.dismissDialogProgress();
    }

    @Override
    public void showOrderSubmitSuccessful() {
        showSingleToast(R.string.tst_order_submit_successful);
        refreshData();
    }

    @Override
    public void showOrderSubmitFailed() {
        showSingleToast(R.string.tst_order_submit_failed);
    }

    @Override
    public void onItemClick(View view, int position) {
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void setPresenter(CartContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    public void showPasswordVerifyFailed() {
        new MaterialDialog.Builder(getContext())
                .content("密码验证失败")
                .show();
    }

    @OnClick(R.id.btn_cart_confirm)
    public void onClick() {
        new MaterialDialog.Builder(getContext())
                .title("验证密码")
                .input("输入验证密码", null, false, new MaterialDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {

                    }
                })
                .inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)
                .positiveText(R.string.general_confirm)
                .negativeText(R.string.general_cancel)
                .positiveColor(getResources().getColor(R.color.pay_confirm_enabled))
                .negativeColor(Color.GRAY)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        mPresenter.orderSubmit(dialog.getInputEditText().getText().toString());
                    }
                })
                .show();
    }
}
